﻿using Hhmocon.Mes.Application;
using Hhmocon.Mes.Application.Base;
using Hhmocon.Mes.Repository.Domain;
using Hhmocon.Mes.Util;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;


namespace Hhmocon.Mes.WebApi.Controllers.Base
{
    [Route("api/[controller]/[action]")]
    [EnableCors(PolicyName = "_hhmoconmesAllowSpecificOrigins")]
    [ApiController]

    public class RoboticEquipmentController : ControllerBase
    {
        private RoboticEquipmentApp _app;
        private PikachuApp _pikachuApp;

        public RoboticEquipmentController(RoboticEquipmentApp app, PikachuApp picachuRepository)
        {
            _app = app;
            _pikachuApp = picachuRepository;
        }


        /// <summary>
        /// 得到所有机械手设备列表分页数据
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<PageData> GetList(PageReq req)
        {
            var result = new Response<PageData>();
            try
            {
                PageData pd = new PageData();
                long lcount = 0;
                pd.Data = _pikachuApp.GetList<robotic_equipment>(req, ref lcount);
                pd.Total = lcount;
                result.Result = pd;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据条件得到全部列表数据
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<PageData> GetAll(PageReq req)
        {
            var result = new Response<PageData>();
            try
            {
                PageData pd = new PageData();
                long lcount = 0;
                pd.Data = _pikachuApp.GetAll<robotic_equipment>(req, ref lcount);
                pd.Total = lcount;
                result.Result = pd;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Update(robotic_equipment obj)
        {
            var result = new Response<string>();
            try
            {
                result.Result = obj.sys_equipment_id;

                if (!_pikachuApp.Update(obj))
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "更新失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 根据设备序列号删除数据  可批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string[]> Delete(string[] ids)
        {
            var result = new Response<string[]>();
            try
            {
                result.Result = ids;

                if (!_pikachuApp.Delete_Mask<robotic_equipment>(ids))
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 新建设备信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Create(robotic_equipment obj)
        {
            var result = new Response<string>();
            try
            {
                robotic_equipment data = _app.Insert(obj);
                if (data != null)
                {
                    result.Result = data.sys_equipment_id;
                }
                else
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "数据写入失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }



        /// <summary>
        ///  根据设备序列号获取明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<robotic_equipment> GetDetail(string id)
        {
            var result = new Response<robotic_equipment>();
            try
            {
                if (id == null)
                {
                    result.Message = "id不能为空";
                    return result;
                }
                robotic_equipment robotic= _pikachuApp.GetById<robotic_equipment>(id);
                if (robotic == null)
                {
                    result.Message = "该id设备不存在";
                    return result;
                }

                if (robotic.state_operating== "0x0002")
                {
                    int rd = new Random().Next(0, 10);
                    robotic.run_production = robotic.run_production + rd;
                    robotic.run_production_total = robotic.run_production_total+ rd ;
                }
                robotic.time_boot = DateTime.Now.Hour * 60 + DateTime.Now.Minute - 450;
                robotic.time_runtime = DateTime.Now.Hour * 60 + DateTime.Now.Minute - 480;
                robotic.time_tolal_boot = 9000 + robotic.time_runtime;
                robotic.time_tolal_runtime = 8500 + robotic.time_tolal_boot;
                 _pikachuApp.Update(robotic);
                result.Result = robotic;
               
            }
            
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
    }
}
